package com.itany.shop.dao.impl;

import com.itany.shop.dao.ProductDao;
import com.itany.shop.entity.Product;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.rowmapper.ProductRowMapper;
import com.itany.shop.util.JDBCTemplate;

import java.util.List;

/**
 * @author 石小俊
 * @date 2023年10月26日 9:22
 */
public class ProductDaoImpl implements ProductDao {

    private JDBCTemplate jdbcTemplate = (JDBCTemplate) ObjectFactory.getObject("jdbc");

    @Override
    public List<Product> selectAll() {
        String sql = new StringBuffer()
                .append(" select id,name,price ")
                .append(" from t_product")
                .toString();
        List<Product> products = jdbcTemplate.query(sql, new ProductRowMapper());
        return products;
    }

    @Override
    public Product selectById(Integer id) {
        String sql = new StringBuffer()
                .append(" select id,name,price ")
                .append(" from t_product")
                .append(" where id = ? ")
                .toString();
        List<Product> products = jdbcTemplate.query(sql, new ProductRowMapper(),id);
        return products.isEmpty() ? null : products.get(0);
    }
}
